package com.community.demo.controller;

import com.community.demo.domain.Goods;
import com.community.demo.domain.User;
import com.community.demo.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @GetMapping("/goodsList")
    public ModelAndView goodsList(){
        List<Goods> goodsList = goodsService.getAllGoods();
        return new ModelAndView("index", "goodsList", goodsList);
    }

    @PutMapping("/updateGoods")
    public String updateGoods(Goods goods){
        goodsService.saveAndFlush(goods);
        return "redirect:/goods";
    }

    @PostMapping("/addGoods")
    public String addGoods(Goods goods){
        goodsService.saveAndFlush(goods);
        return "redirect:/goods";
    }

    @PutMapping("/deleteGoods")
    public String deleteGoods(long id){
        goodsService.deleteGoods(id);
        return "redirect:/goods";
    }

    @GetMapping("/getGoodsById")
    public ModelAndView getGoodsById(long id){
        Goods goods = goodsService.findGoodsById(id);
        return new ModelAndView("", "goods", goods);
    }

}
